<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <!-- 导入axios -->
    <script src="./node_modules/axios/dist/axios.js"></script>
    <style>
        body{
            padding: 30px;
        }
    </style>

   
</head>
<body>
    <h1>修改用户</h1>
    用户名：<input type="text" class="username"><br>
    密码：<input type="text" class="password"><br>
    <button class="btn">提交</button>

    <script>
        //根据location对象，获取带过来的id
        const id = location.search.replace('?','').split('=')[1]

        //发送axios请求，拿到要修改的用户，回显函数
        axios.get(`http://localhost:3000/users/${id}`).then(res => {
            console.log(res.data)
            //回显数据
            document.querySelector('.username').value = res.data.username
            document.querySelector('.password').value = res.data.password
        })
        //点击实现修改操作
        document.querySelector('.btn').addEventListener('click',function(){
            //获取用户名和密码
            const username = document.querySelector('.username').value
            const password = document.querySelector('.password').value

            //发送post请求
            axios({
                method:'put',
                url:`http://localhost:3000/users/${id}`,
                data:{username:username,password:password}
            }).then(res =>{
                //跳转到列表页
                location.herf = './index.html'
            })
        })
    </script>
</body>
</html>